package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.ActiveInfoVListRequestFilter;
import com.mmlc.network.responsebean.ActiveInfoVListResponseBean;
import com.mmlc.network.responsebean.InformationDataBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;

/**
 * description: 大V推荐资讯列表
 * by king
 * Created at 2016/8/26 13:09
 */
public class RecommendInfoActivity extends InterActivity implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener {

    @Override
    protected int initContentView() {
        return R.layout.activity_recommend_info;
    }

    String user_id = "";

    @Override
    protected void initExtra() {
        super.initExtra();

        user_id = getIntent().getStringExtra("user_id");
    }

    PullToRefreshListView refresh_list;
    ArrayList<InformationDataBean> datas;
    FashionInformationAdapter adapter;

    @Override
    protected void initView() {
        refresh_list = ViewUtils.getView(activity, R.id.refresh_list);
        datas = new ArrayList<>();
        adapter = new FashionInformationAdapter(context, datas);
        refresh_list.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
        refresh_list.setOnRefreshListener(this);
        refresh_list.setOnItemClickListener(this);
    }

    int pageNow = 0;
    boolean isOk = true;

    @Override
    protected void initInfo() {
        super.initInfo();
        isOk = false;

        ActiveInfoVListRequestFilter filter = new ActiveInfoVListRequestFilter(activity);
        filter.requestBean.user_id = user_id;
        filter.requestBean.pageNow = String.valueOf(this.pageNow);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ActiveInfoVListResponseBean>() {
            @Override
            public void onSuccess(ActiveInfoVListResponseBean result) {
                loadComplete();
                if (pageNow > 0 && (result.getData().getData() == null || result.getData().getData().size() == 0)) {
                    ToastUtils.showToast("没有更多了");
                    refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    return;
                }
                if (pageNow == 0) datas.clear();
                datas.addAll(result.getData().getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                loadComplete();
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    private void loadComplete() {
        isOk = true;
        refreshHandler.sendEmptyMessage(1);
    }

        Handler refreshHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 0) {
                if (!isOk) {
                    refresh_list.onRefreshComplete();
                    isOk = true;
                }
            } else if (msg.what == 1) {
                refresh_list.onRefreshComplete();
            }
        }
    };

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) throws InterruptedException {
        if (isOk) {
            pageNow = 1;
            initInfo();
            refreshHandler.sendEmptyMessageDelayed(0, 3000);
        }
        refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        if (isOk) {
            pageNow++;
            initInfo();
            refreshHandler.sendEmptyMessageDelayed(0, 3000);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("id", datas.get(position - 1).getId())
                .putExtra("url", datas.get(position - 1).getLink_url())
                .putExtra("shareImageUrl", datas.get(position - 1).getImg())
                .putExtra("title", datas.get(position - 1).getName())
                .putExtra("type", 1);
        startActivity(intent);
    }
}
